/*
 * FMSTR_drv_uart.c
 *
 *  Created on: Feb 20, 2020
 *      Author: songl
 */

#include "freemaster.h"
#include "freemaster_private.h"

/* Numeric identifier to help pre-processor to identify whether our driver is used or not. */
#define FMSTR_SERIAL_STM32_UART_ID 1

#if (FMSTR_MK_IDSTR(FMSTR_SERIAL_DRV) == FMSTR_SERIAL_STM32_UART_ID)

#if (!(FMSTR_DISABLE))

#include "stm32f1xx_hal.h"

#include "freemaster_protocol.h"
#include "freemaster_serial.h"

/******************************************************************************
* Adapter configuration
******************************************************************************/
extern UART_HandleTypeDef huart1;
/***********************************
*  local variables
***********************************/

/***********************************
*  local function prototypes
***********************************/

/* Interface function - Initialization of serial UART driver adapter */
static FMSTR_BOOL _FMSTR_SerialuartInit(void);
static void _FMSTR_SerialuartEnableTransmit(FMSTR_BOOL enable);
static void _FMSTR_SerialuartEnableReceive(FMSTR_BOOL enable);
static void _FMSTR_SerialuartEnableTransmitInterrupt(FMSTR_BOOL enable);
static void _FMSTR_SerialuartEnableTransmitCompleteInterrupt(FMSTR_BOOL enable);
static void _FMSTR_SerialuartEnableReceiveInterrupt(FMSTR_BOOL enable);
static FMSTR_BOOL _FMSTR_SerialuartIsTransmitRegEmpty(void);
static FMSTR_BOOL _FMSTR_SerialuartIsReceiveRegFull(void);
static FMSTR_BOOL _FMSTR_SerialuartIsTransmitterActive(void);
static void _FMSTR_SerialuartPutChar(FMSTR_BCHR  ch);
static FMSTR_BCHR _FMSTR_SerialuartGetChar(void);
static void _FMSTR_SerialuartFlush(void);

/***********************************
*  global variables
***********************************/
/* Interface of this serial UART driver */
const FMSTR_SERIAL_DRV_INTF FMSTR_SERIAL_STM32_UART =
{
    .Init                           = _FMSTR_SerialuartInit,
    .EnableTransmit                 = _FMSTR_SerialuartEnableTransmit,
    .EnableReceive                  = _FMSTR_SerialuartEnableReceive,
    .EnableTransmitInterrupt        = _FMSTR_SerialuartEnableTransmitInterrupt,
    .EnableTransmitCompleteInterrupt= _FMSTR_SerialuartEnableTransmitCompleteInterrupt,
    .EnableReceiveInterrupt         = _FMSTR_SerialuartEnableReceiveInterrupt,
    .IsTransmitRegEmpty             = _FMSTR_SerialuartIsTransmitRegEmpty,
    .IsReceiveRegFull               = _FMSTR_SerialuartIsReceiveRegFull,
    .IsTransmitterActive            = _FMSTR_SerialuartIsTransmitterActive,
    .PutChar                        = _FMSTR_SerialuartPutChar,
    .GetChar                        = _FMSTR_SerialuartGetChar,
    .Flush                          = _FMSTR_SerialuartFlush,
};

/**************************************************************************//*!
*
* @brief    Serial communication initialization
*
******************************************************************************/
static FMSTR_BOOL _FMSTR_SerialuartInit(void)
{
    ;//soong
    return FMSTR_TRUE;
}

/**************************************************************************//*!
*
* @brief    Enable/Disable Serial transmitter
*
******************************************************************************/

static void _FMSTR_SerialuartEnableTransmit(FMSTR_BOOL enable)
{
	;//soong
}

/**************************************************************************//*!
*
* @brief    Enable/Disable Serial receiver
*
******************************************************************************/

static void _FMSTR_SerialuartEnableReceive(FMSTR_BOOL enable)
{
	;//soong
}

/**************************************************************************//*!
*
* @brief    Enable/Disable interrupt from transmit register empty event
*
******************************************************************************/

static void _FMSTR_SerialuartEnableTransmitInterrupt(FMSTR_BOOL enable)
{
	;//soong
}

/**************************************************************************//*!
*
* @brief    Enable/Disable interrupt from transmit complete event
*
******************************************************************************/

static void _FMSTR_SerialuartEnableTransmitCompleteInterrupt(FMSTR_BOOL enable)
{
	;//soong
}

/**************************************************************************//*!
*
* @brief    Enable/Disable interrupt from receive register full event
*
******************************************************************************/

static void _FMSTR_SerialuartEnableReceiveInterrupt(FMSTR_BOOL enable)
{
     ;//soong
}

/**************************************************************************//*!
*
* @brief    Returns TRUE if the transmit register is empty, and it's possible to put next char
*
******************************************************************************/

static FMSTR_BOOL _FMSTR_SerialuartIsTransmitRegEmpty(void)
{
   // uint32_t sr = uart_GetStatusFlags(fmstr_serialBaseAddr);
   //
   // return (sr & kuart_TxDataRegEmptyFlag);
	;
	return 1;
}

/**************************************************************************//*!
*
* @brief    Returns TRUE if the receive register is full, and it's possible to get received char
*
******************************************************************************/

static FMSTR_BOOL _FMSTR_SerialuartIsReceiveRegFull(void)
{
    //uint32_t sr = uart_GetStatusFlags(fmstr_serialBaseAddr);
//
    //return (sr & kuart_RxDataRegFullFlag);
	;
	return 1;
}

/**************************************************************************//*!
*
* @brief    Returns TRUE if the transmitter is still active
*
******************************************************************************/

static FMSTR_BOOL _FMSTR_SerialuartIsTransmitterActive(void)
{
    //uint32_t sr = uart_GetStatusFlags(fmstr_serialBaseAddr);
//
    //return !(sr & kuart_TransmissionCompleteFlag);
	;
	return 1;
}

/**************************************************************************//*!
*
* @brief    The function puts the char for transmit
*
******************************************************************************/

static void _FMSTR_SerialuartPutChar(FMSTR_BCHR  ch)
{
    //uart_WriteByte(fmstr_serialBaseAddr, ch);
    HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 1);
}

/**************************************************************************//*!
*
* @brief    The function gets the received char
*
******************************************************************************/
static FMSTR_BCHR _FMSTR_SerialuartGetChar(void)
{
static FMSTR_BCHR ch;
    return HAL_UART_Receive(&huart1, (uint8_t *)&ch, 1, 1);
}

/**************************************************************************//*!
*
* @brief    The function sends buffered data
*
******************************************************************************/
static void _FMSTR_SerialuartFlush(void)
{
}

/**************************************************************************//*!
*
* @brief    Assigning FreeMASTER communication module base address
*
******************************************************************************/

/**************************************************************************//*!
*
* @brief    API: Interrupt handler call
*
* This Interrupt Service Routine handles the UART  interrupts for the FreeMASTER
* driver. In case you want to handle the interrupt in the application yourselves,
* call the FMSTR_ProcessSerial function which
* does the same job but is not compiled as an Interrupt Service Routine.
*
* In poll-driven mode (FMSTR_POLL_DRIVEN) this function does nothing.
*
******************************************************************************/

void FMSTR_SerialIsr()
{
    /* process incomming or just transmitted byte */
    #if (FMSTR_LONG_INTR) || (FMSTR_SHORT_INTR)
        FMSTR_ProcessSerial();
    #endif
}

#else /* (!(FMSTR_DISABLE)) */

void FMSTR_SerialSetBaseAddress(uart_Type *base)
{
    FMSTR_UNUSED(base);
}

void FMSTR_SerialIsr(void)
{
}

#endif /* (!(FMSTR_DISABLE)) */
#endif /* (FMSTR_MK_IDSTR(FMSTR_SERIAL_DRV) == FMSTR_SERIAL_STM32_UART_ID) */
